package org.example.controller;

import org.example.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    //普通参数
    //@RequestParam("username")-请求参数和形参的映射
    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(@RequestParam("username") String name, int age) {
        System.out.println("BookController-commonParam.." + "name:" + name + ",age:" + age);
        return "{\"BookController\":\"commonParam\",\"name\":\"" + name + "\",\"age\":\"" + age + "\"}";
    }

    //pojo参数
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user) {
        System.out.println("BookController-pojoParam..user:" + user);
        return user.toString();
    }

    //嵌套pojo
    @RequestMapping("/pojoContainParam")
    @ResponseBody
    public String pojoContainParam(User user) {
        System.out.println("BookController-pojoContainParam..user:" + user);
        return user.toString();
    }

    //数组参数
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes) {
        System.out.println("BookController-arrayParam..array:" + Arrays.toString(likes));
        return Arrays.toString(likes);
    }

    //集合参数
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> likes) {
        System.out.println("BookController-listParam..list:" + likes);
        return likes.toString();
    }

    //json-集合
    @RequestMapping("/jsonListParam")
    @ResponseBody
    public String jsonListParam(@RequestBody List<String> likes) {
        System.out.println("BookController-jsonListParam..list:" + likes);
        return likes.toString();
    }

    //json-pojo
    @RequestMapping("/jsonPojoParam")
    @ResponseBody
    public String jsonPojoParam(@RequestBody User user) {
        System.out.println("BookController-jsonPojoParam..user:" + user);
        return user.toString();
    }

    //json-pojo集合
    @RequestMapping("/jsonPojoListParam")
    @ResponseBody
    public String jsonPojoListParam(@RequestBody List<User> users) {
        System.out.println("BookController-jsonPojoListParam..users:" + users);
        return users.toString();
    }

    //日期参数
    @RequestMapping("/dateParam")
    @ResponseBody
    public String dateParam(Date date,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                            @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date2) {
        System.out.println("BookController-dateParam..date:" + date);
        System.out.println("BookController-dateParam..date:" + date1);
        System.out.println("BookController-dateParam..date:" + date2);
        return "date";
    }
}
